/**
 * 文件名：SysProperty.java
 *
 * 版本信息：
 * 日期：2009-6-17
 * Copyright dp Corporation 2009 
 * 版权所有 2009All rights reserved.
 * 创建者：wht
 *
 */
package com.dp.pub;

import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.dp.util.InterUtil;


/**
 * 
 * 项目名称：book
 * 类名称：SysProperty
 * 类描述：获取系统参数，可以从文件获取，也可以从数据库加载，
 *         并当文件被修改时自动加载新参数
 *        
 * 创建人：Administrator
 * 创建时间：2009-6-17 下午02:26:15
 * 修改人：Administrator
 * 修改时间：2009-6-17 下午02:26:15
 * 修改备注：
 */
public class SysProperty {

	 private InitialContext ic;
	private Map cache;
	private static long lasttime = 0l;
	private static Properties prop = new Properties();
	// private static String fn="JNDIMAP.properties";
	private static String syspro = "jboss.server.home.url";// "jboss.server.home.dir";
	private static String syspro1 = "/";
	private static boolean userpro = false;
	private static SysProperty me;
	private static File f = null;
	private static String path = "";
	private static String uploadpath = "";
	
	static{
		me=new SysProperty();
		//setFilename();	
		reload();
	}

	public static void load() {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs1 = null;
		String sql = null;
		try {
			conn = HUtil.getConn();
			stmt = conn.createStatement();
			sql = "select concat(pfirstname,plastname) as pkey,pvalue from t_sys_sysparam";
			rs1 = stmt.executeQuery(sql);
			while (rs1.next())
				prop.put(rs1.getString(1), rs1.getString(2));// bl.add(rs1.getString(1));
			if (rs1 != null) {
				rs1.close();
				rs1 = null;
			}
			if (stmt != null) {
				stmt.close();
				stmt = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs1 != null) {
					rs1.close();
					rs1 = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void reload() {
		prop.clear();
		load();
	}

	static public void setFilename() {
		URL fi = null;
		// fn="JNDIMAP.properties";
		try {
			if (userpro)
				fi = new URL(System.getProperty(syspro));
			else
				fi = me.getClass().getResource("/");

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		path = fi.getPath();
		// fn=path+fn;
		uploadpath = path + "webupfile";
		f = new File(path + "sysparam.properties");
		File f = new File(uploadpath);
		if (!f.exists())
			f.mkdir();
	}

	static public String getUpFileDir()// throws FileNotFoundException
	{
		return uploadpath;
	}

	static public String getCarrier(String pr) {

		String temp = getPrep("carrier");
		int fc = temp.indexOf(pr);
		return temp.substring(fc + 4, fc + 5);
	}

	static public Properties getPrep() {

		return prop;

	}

	static public String getPrep(String key)// throws FileNotFoundException
	{
		String v = null;

		try {
			/*if (lasttime != f.lastModified()) {
				lasttime = f.lastModified();
				InputStream in = new java.io.FileInputStream(f);
				prop.clear();
				prop.load(in);
				in.close();

			}*/
			v = prop.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}

    private SysProperty()   {
        try {
			ic = new InitialContext();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        cache = Collections.synchronizedMap(new HashMap());
    }
	public static SysProperty getInstance() {
		return me;
	}
	 public Object getRemoteObject(String jndiname,String ip,String port)throws NamingException
	    {
	        Properties prop1 = new Properties();
	        prop1.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
		prop1.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");
	        prop1.setProperty("java.naming.provider.url", ip+":"+port);//"172.16.3.93:1099");	
	        InitialContext ctx1 = new InitialContext(prop1);        
	        return ctx1.lookup(jndiname);
	    }
	     public Object getRemoteObject(String jndiname) throws NamingException 
	    {
	         Properties prop1 = this.getPrep();
	        InitialContext ctx1 = new InitialContext(prop1);        
	        return ctx1.lookup(jndiname); 
	    }
	    public Object getObject(String jndiName) throws NamingException {
	        return lookup(jndiName);
	    }
	    private Object lookup(String jndiName) throws NamingException {
	        String name=getPrep(jndiName);
	         name=name==null?jndiName:name;
	        Object cachedObj = cache.get(name);
	        if (cachedObj == null) {
	            cachedObj = ic.lookup(name);
	            cache.put(name, cachedObj);
	        }
	        return cachedObj;
	    }
	    
	    
		public static int maxclientcount(Integer maxclientcount) {
			Connection conn = null;
			Statement stmt = null;
			ResultSet rs1 = null;
			String sql = null;
			int r=0;
			try {
				conn = HUtil.getConn();
				stmt = conn.createStatement();
				sql = "select count(clientid) from news_clientinfo";
				rs1 = stmt.executeQuery(sql);
				if (rs1.next())
					r=rs1.getInt(1);
					if(maxclientcount==null||r<maxclientcount){
						r=0;
					}
				if (rs1 != null) {
					rs1.close();
					rs1 = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (rs1 != null) {
						rs1.close();
						rs1 = null;
					}
					if (stmt != null) {
						stmt.close();
						stmt = null;
					}
					if (conn != null) {
						conn.close();
						conn = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			return r;
		}
}
